//
//  RCActivityDetailHeaderCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCActivityDetailHeaderCell.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "NSString+NSString_Ex_.h"
#import "RCActivityDetailDataModel.h"
#import "UIImageView+WebCache.h"


#define kDefaultimageFile        @"image_recommendFood_image_bg.png"
#define kLineImageFile           @"image_line_type3.png"
#define kBGImageFile             @"image_discount_cell_bg.png"
#define kBaomingImageFile        @"image_activityDetail_baoming.png"
#define kDiscountNumImageFile    @"image_activityDetail_discount_num.png"
#define kPriceBGImageFile        @"image_activityDetail_price_bg.png"

@interface RCActivityDetailHeaderCell()

@property(nonatomic,retain)UIView      *backgroundColorView;
@property(nonatomic,retain)UIImageView *defaultImageView;
@property(nonatomic,retain)UIImageView *picImageView;
@property(nonatomic,retain)UIButton    *transParentButton;
@property(nonatomic,retain)UILabel     *activityNameLabel;
@property(nonatomic,retain)UILabel     *activityContentLabel;
@property(nonatomic,retain)UILabel     *activityTimeLabel;
@property(nonatomic,retain)UILabel     *originPriceLabel;
@property(nonatomic,retain)UILabel     *presentPriceLabel;
@property(nonatomic,retain)UILabel     *discountNumLabel;
@property(nonatomic,retain)UIButton    *baomingImageView;
@property(nonatomic,retain)UILabel     *signupNumberLabel;
@property(nonatomic,retain)UILabel     *signupWordLabel;
@property(nonatomic,retain)UILabel     *leftTimeLabel;
@property(nonatomic,retain)UIImageView *lineImageView;

@end

@implementation RCActivityDetailHeaderCell

@synthesize backgroundColorView = _backgroundColorView;
@synthesize defaultImageView = _defaultImageView;
@synthesize picImageView = _picImageView;
@synthesize transParentButton = _transParentButton;
@synthesize activityNameLabel = _activityNameLabel;
@synthesize activityContentLabel = _activityContentLabel;
@synthesize activityTimeLabel = _activityTimeLabel;
@synthesize originPriceLabel = _originPriceLabel;
@synthesize presentPriceLabel = _presentPriceLabel;
@synthesize discountNumLabel = _discountNumLabel;
@synthesize baomingImageView = _baomingImageView;
@synthesize signupNumberLabel = _signupNumberLabel;
@synthesize signupWordLabel = _signupWordLabel;
@synthesize leftTimeLabel = _leftTimeLabel;
@synthesize lineImageView = _lineImageView;
@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
//        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
//        [self.contentView setBackgroundColorWithImage:bgImage];
        [self createPlaceHolderView];
    }
    return self;
}

-(void)dealloc{
    self.delegate = nil;
    self.defaultImageView = nil;
    self.picImageView = nil;
    self.transParentButton = nil;
    self.activityTimeLabel = nil;
    self.activityContentLabel = nil;
    self.activityNameLabel = nil;
    self.originPriceLabel = nil;
    self.presentPriceLabel = nil;
    self.discountNumLabel = nil;
    self.baomingImageView = nil;
    self.signupNumberLabel = nil;
    self.signupWordLabel = nil;
    self.leftTimeLabel = nil;
    self.lineImageView = nil;
    self.backgroundColorView = nil;
    [super dealloc];
}

-(void)createPlaceHolderView{
    
    _backgroundColorView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 193)];
    _backgroundColorView.backgroundColor = [UIColor colorWithHex:0xfff6f6f6];
    [self addSubview:_backgroundColorView];
    
    _activityNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(14, 14, 300, 19)];
    _activityNameLabel.backgroundColor = [UIColor clearColor];
    _activityNameLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:19];
    _activityNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    [self addSubview:_activityNameLabel];
    
    _activityContentLabel = [[UILabel alloc]initWithFrame:CGRectMake(14, 38, 300, 14)];
    _activityContentLabel.backgroundColor = [UIColor clearColor];
    _activityContentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _activityContentLabel.textColor = [UIColor colorWithHex:0xff505050];
    [self addSubview:_activityContentLabel];
    
    
    _defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 60, 169, 116)];
    _defaultImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultimageFile ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_defaultImageView setImage:defaultImage];
    [self addSubview:_defaultImageView];
    
    _picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 63, 163, 110)];
    _picImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_picImageView];
    
     _transParentButton = [[UIButton alloc]initWithFrame:_defaultImageView.frame];
    _transParentButton.backgroundColor = [UIColor clearColor];
    [_transParentButton addTarget:self action:@selector(transparentButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _transParentButton.hidden = YES;
    [self addSubview:_transParentButton];
    
    UIImageView *priceImageView = [[UIImageView alloc]initWithFrame:CGRectMake(193, 60, 114, 29)];
    priceImageView.backgroundColor = [UIColor clearColor];
    [priceImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kPriceBGImageFile ofType:nil]]];
    
    _presentPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 7, 55, 15)];
    _presentPriceLabel.backgroundColor = [UIColor clearColor];
    _presentPriceLabel.textColor = [UIColor whiteColor];
    _presentPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    [priceImageView addSubview:_presentPriceLabel];
    
    _originPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 11, 60, 12)];
    _originPriceLabel.backgroundColor = [UIColor clearColor];
    _originPriceLabel.textColor = [UIColor whiteColor];
    _originPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [priceImageView addSubview:_originPriceLabel];
    
    UIView *priceLineView = [[UIView alloc]initWithFrame:CGRectMake(68, 16, 30, 1)];
    priceImageView.backgroundColor = [UIColor whiteColor];
    [priceImageView addSubview:priceLineView];
    [priceLineView release];
    
    [self addSubview:priceImageView];
    [priceImageView release];
    
    UIImageView *discountNumImageView = [[UIImageView alloc]initWithFrame:CGRectMake(263, 56, 22, 15)];
    discountNumImageView.backgroundColor = [UIColor clearColor];
    [discountNumImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDiscountNumImageFile ofType:nil]]];
    
    _discountNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, 22, 9)];
    _discountNumLabel.backgroundColor = [UIColor clearColor];
    _discountNumLabel.textColor = [UIColor whiteColor];
    _discountNumLabel.font = [UIFont fontWithName:kFontNameHelvetica size:9];
    [discountNumImageView addSubview:_discountNumLabel];
    [self addSubview:discountNumImageView];
    [discountNumImageView release];
    
    _baomingImageView = [[UIButton alloc]initWithFrame:CGRectMake(204, 93, 93, 22)];
    _baomingImageView.backgroundColor = [UIColor clearColor];
    [_baomingImageView setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBaomingImageFile ofType:Nil]] forState:UIControlStateNormal];
    [_baomingImageView addTarget:self action:@selector(baomingButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_baomingImageView];
    
    _signupNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(220, 124, 20, 15)];
    _signupNumberLabel.backgroundColor = [UIColor clearColor];
    _signupNumberLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    _signupNumberLabel.textColor = [UIColor colorWithHex:0xffff8200];
    [self addSubview:_signupNumberLabel];
    
    _signupWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(240, 126, 70, 13)];
    _signupWordLabel.backgroundColor = [UIColor clearColor];
    _signupWordLabel.textColor = [UIColor colorWithHex:0xff989797];
    _signupWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:13];
    _signupWordLabel.text = @"人已参加";
    [self addSubview:_signupWordLabel];
    
    _leftTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(183, 141, 137, 13)];
    _leftTimeLabel.backgroundColor = [UIColor clearColor];
    _leftTimeLabel.textColor = [UIColor colorWithHex:0xff989797];
    _leftTimeLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:13];
    _leftTimeLabel.textAlignment = UITextAlignmentCenter;
    [self addSubview:_leftTimeLabel];
    
    UIImageView *halfLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(185, 156, 133, 1)];
    halfLineImageView.backgroundColor = [UIColor clearColor];
    [halfLineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
    [self addSubview:halfLineImageView];
    [halfLineImageView release];
    
    _activityTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(183, 160, 137, 11)];
    _activityTimeLabel.backgroundColor = [UIColor clearColor];
    _activityTimeLabel.textColor = [UIColor colorWithHex:0xff7b7a7a];
    _activityTimeLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:11];
    _activityTimeLabel.textAlignment = UITextAlignmentCenter;
    [self addSubview:_activityTimeLabel];
    
    _lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 192, 292, 1)];
    _lineImageView.backgroundColor = [UIColor clearColor];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
    [self addSubview:_lineImageView];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)bindData:(RCActivityDetailDataModel *)dataModel{
    if (nil == dataModel) {
        return;
    }
    
    _activityNameLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.activityName]) {
        _activityNameLabel.hidden = NO;
        _activityNameLabel.text = dataModel.activityName;
    }
    
    _activityContentLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.activityContent]) {
        _activityContentLabel.hidden = NO;
        _activityContentLabel.text = dataModel.activityContent;
    }
    _transParentButton.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.activityPicUrl]) {
        _transParentButton.hidden = NO;
        [_picImageView setImageWithURL:[NSURL URLWithString:dataModel.activityPicUrl] placeholderImage:nil];
    }
    
    _presentPriceLabel.hidden = YES;
    if (dataModel.activityPresentPrice > 0) {
        _presentPriceLabel.hidden = NO;
        _presentPriceLabel.text = [NSString stringWithFormat:@"￥%d",dataModel.activityPresentPrice];
    }
    
    _originPriceLabel.hidden = YES;
    if (dataModel.activityOriginPrice > 0) {
        _originPriceLabel.hidden = NO;
        _originPriceLabel.text = [NSString stringWithFormat:@"￥%d",dataModel.activityOriginPrice];
    }
    
    _discountNumLabel.hidden = YES;
    if (dataModel.activityPresentPrice > 0 && dataModel.activityOriginPrice > 0) {
        _discountNumLabel.hidden = NO;
        CGFloat num = (CGFloat)dataModel.activityPresentPrice/(CGFloat)dataModel.activityOriginPrice*10;
        _discountNumLabel.text = [NSString stringWithFormat:@"%.1f折",num];
    }
    
    _signupNumberLabel.hidden = YES;
    _signupWordLabel.hidden = YES;
    if (dataModel.activitySignUp >=0) {
        _signupNumberLabel.hidden = NO;
        _signupWordLabel.hidden = NO;
        _signupNumberLabel.text = [NSString stringWithFormat:@"%d",dataModel.activitySignUp];
        
        CGSize size = [_signupNumberLabel.text sizeWithFont:_signupNumberLabel.font constrainedToSize:CGSizeMake(200, 20)];
        CGRect numberRect = _signupNumberLabel.frame;
        numberRect.size.width = size.width;
        _signupNumberLabel.frame = numberRect;
        
        CGRect wordRect = _signupWordLabel.frame;
        wordRect.origin.x = CGRectGetMaxX(_signupNumberLabel.frame);
        _signupWordLabel.frame = wordRect;
    }
    
    
    
    _activityTimeLabel.hidden = YES;
    _leftTimeLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.activityEndTime]) {
        _activityTimeLabel.hidden = NO;
        _leftTimeLabel.hidden = NO;
        _activityTimeLabel.text = [NSString stringWithFormat:@"截止时间：%@",dataModel.activityEndTime];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        
        NSDate *startDate = [[NSDate alloc]init];
        startDate = [formatter dateFromString:dataModel.activityEndTime];
        
        NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        
        NSDateComponents *diffDate = [calendar components:NSDayCalendarUnit fromDate:startDate toDate:[NSDate date] options:0];
        
        
        
        NSInteger day = abs(diffDate.day);
        if (day > 3) {
            _leftTimeLabel.text = @"剩余3天以上";
        }else{
            _leftTimeLabel.text = [NSString stringWithFormat:@"剩余%d天",day];
        }
        
        [formatter release];
        [startDate release];
        [calendar release];
    }
}

-(void)baomingButtonClicked{
    if (_delegate && [_delegate respondsToSelector:@selector(gotoOrderActivityVC)]) {
        [_delegate gotoOrderActivityVC];
    }
}

-(void)transparentButtonClicked{
    if (_delegate && [_delegate respondsToSelector:@selector(gotoActivityPictureAlbumView)]) {
        [_delegate gotoActivityPictureAlbumView];
    }
}

@end
